﻿using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;
using System.Xml;
using System.IO;

namespace ServBus.ListForm2010.Utility
{
    class DmsCommonType
    {
        protected InfoPathContext mContext;
        protected FieldInfo mFieldInfo;
        protected List<string> mFileList;

        public DmsCommonType(InfoPathContext context, FieldInfo fieldInfo)
        {
            mContext = context;
            mFieldInfo = fieldInfo;
            mFileList = new List<string>();
        }

        //添加xsd文件
        public void AddXsdFile(string fileName, string content, bool hasChoiceAlready)
        {
            if (!hasChoiceAlready)
            {
                Utility.ConvertFileEncoding(NameSpaceConstants.TemplateRootPath + fileName, mContext.Path + fileName, Encoding.UTF8);
            }
            XmlDocument xmlDoc = new XmlDocument();
            string choiceXsdPath = mContext.Path + fileName;
            xmlDoc.Load(choiceXsdPath);

            XPathNavigator nav = xmlDoc.CreateNavigator();
            XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
            //命名空间如果错了，不会抱错，不过下面select出来的都是Null
            manager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");

            XPathNavigator subIter = nav.SelectSingleNode("/xsd:schema/xsd:element/xsd:complexType/xsd:all", manager);
            subIter.AppendChild(content);
            Utility.SaveDoc(xmlDoc, mContext.Path + fileName);

            AddToFileList(fileName);
            mContext.AddSavedFile(fileName);
        }

        public void AddFileListRefToManifest()
        {
            string content = string.Empty;
            foreach (string fileName in mFileList)
            {
                switch (fileName)
                {
                    case "choices.xsd":
                        content = string.Format(BaseTypeStrings.ChoiceXsdRefInManifest, BaseTypeStrings.DropdownDataSouceName);
                        AddOneFileRefToManifest(fileName, content);
                        break;

                    case "choices.xml":
                        content = BaseTypeStrings.ChoiceXmlRefInManifest;
                        AddOneFileRefToManifest(fileName, content);
                        break;

                    default:
                        break;
                }
            }
        }

        public void AddOneFileRefToManifest(string fileName, string content)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string manifestPath = mContext.Path + "manifest.xsf";
            xmlDoc.Load(manifestPath);

            bool beDefined = false;

            XPathNavigator nav = xmlDoc.CreateNavigator();
            XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
            manager.AddNamespace("xsf", "http://schemas.microsoft.com/office/infopath/2003/solutionDefinition");
            XPathNodeIterator iter = nav.Select("/xsf:xDocumentClass/xsf:package/xsf:files/xsf:file", manager);

            while (iter.MoveNext())
            {
                if (iter.Current.GetAttribute("name", string.Empty) == fileName)
                {
                    beDefined = true;
                }
            }

            if (!beDefined)
            {
                XPathNodeIterator subIter = nav.Select("/xsf:xDocumentClass/xsf:package/xsf:files", manager);
                subIter.MoveNext();
                subIter.Current.AppendChild(content);
                Utility.SaveDoc(xmlDoc, mContext.Path + "manifest.xsf");
            }
        }

        public void AddConnectionStrToSampleData(string content)
        {
            XmlDocument doc = new XmlDocument();
            string baseContent = File.ReadAllText(mContext.Path + "sampledata.xml", Encoding.UTF8).Replace(NameSpaceConstants.XSDNamespaceValuePlaceHolder, mContext.XSDSchemaValue);
            doc.LoadXml(baseContent);

            XPathNavigator nav = doc.CreateNavigator();
            XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
            manager.AddNamespace("xd", NameSpaceConstants.XDNamespaceValue);
            manager.AddNamespace("my", mContext.XSDSchemaValue);
            XPathNodeIterator iter = nav.Select("/my:myFields/xd:DataConnection", manager);


            bool ifDefined = false;
            try
            {
                while (iter.MoveNext())
                {
                    if (iter.Current.GetAttribute("Name", string.Empty) == BaseTypeStrings.DropdownDataSouceName)
                    {
                        ifDefined = true;
                    }
                }
            }
            catch { }

            if (!ifDefined)
            {
                nav = doc.CreateNavigator();
                nav.MoveToChild("myFields", mContext.XSDSchemaValue);
                nav.AppendChild(content);

                Utility.SaveDoc(doc, mContext.Path + "sampledata.xml");
            }
        }

        public void AddOneNodeStrToSampledata(string content)
        {
            XmlDocument doc = new XmlDocument();
            string baseContent = File.ReadAllText(mContext.Path + "sampledata.xml", Encoding.UTF8).Replace(NameSpaceConstants.XSDNamespaceValuePlaceHolder, mContext.XSDSchemaValue);
            doc.LoadXml(baseContent);

            XPathNavigator nav = doc.CreateNavigator();
            XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
            manager.AddNamespace("xd", NameSpaceConstants.XDNamespaceValue);
            manager.AddNamespace("my", mContext.XSDSchemaValue);
            XPathNavigator subNav = nav.SelectSingleNode("/my:myFields/xd:DataConnection/root", manager);
            subNav.AppendChild(content);

            Utility.SaveDoc(doc, mContext.Path + "sampledata.xml");
        }

        public void AddDataObjectToManifest(string content)
        {
            string manifestPath = mContext.Path + "manifest.xsf";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(manifestPath);
            XPathNavigator nav = xmlDoc.CreateNavigator();

            bool a = nav.MoveToChild("xDocumentClass", NameSpaceConstants.XSFNameSpaceValue);
            bool b = nav.MoveToChild("dataObjects", NameSpaceConstants.XSFNameSpaceValue);
            if (a)
            {
                XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
                manager.AddNamespace("xsf", "http://schemas.microsoft.com/office/infopath/2003/solutionDefinition");
                XPathNodeIterator iter = nav.Select("/xsf:xDocumentClass/xsf:dataObjects/xsf:dataObject", manager);

                bool defiend = false;
                while (iter.MoveNext())
                {
                    if (iter.Current.GetAttribute("name", string.Empty) == BaseTypeStrings.DropdownDataSouceName)
                    {
                        defiend = true;
                        break;
                    }
                }
                if (!defiend)
                {
                    XPathNavigator childNav = XmlUtility.CreateDocNav(content);
                    childNav.MoveToChild("dataObject", NameSpaceConstants.XSFNameSpaceValue);
                    nav.AppendChild(childNav);
                    Utility.SaveDoc(xmlDoc, manifestPath);
                }
            }
        }

        public void AddToFileList(string fileName)
        {
            if (!mFileList.Contains(fileName))
            {
                mFileList.Add(fileName);
            }
        }
    }
}
